package org.javaymw.springboot.controller.activiti.servicre;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.javaymw.springboot.dto.RoleDto;
import org.javaymw.springboot.dto.UserDto;
import org.javaymw.springboot.dto.UserRoleDto;
import org.javaymw.springboot.service.RoleService;
import org.javaymw.springboot.service.UserRoleService;
import org.javaymw.springboot.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/10/18 16:20
 * @Description:
 */
@Service
public class SyncDataService {

    @Autowired
    protected UserService userService;

    @Autowired
    protected RoleService roleService;

    @Autowired
    protected UserRoleService userRoleService;

    @Autowired
    private IdentityService identityService;

    private static final Logger LOGGER = LoggerFactory.getLogger(SyncDataService.class);

    /**
     * 开启异步支持
     */
    @Async
    public void syncData() {
        LOGGER.info("......同步数据start......");
        long start = System.currentTimeMillis();
        // 同步业务系统用户到activiti用户表中
        List<UserDto> userDtos = userService.list();
        User user = null;
        if (!userDtos.isEmpty()) {
            for (UserDto userDto : userDtos) {
                user = new UserEntity();
                user.setId(userDto.getId().toString());
                user.setFirstName(userDto.getUserName());
                user.setEmail(userDto.getEmail());
                identityService.deleteUser(userDto.getId().toString());
                identityService.saveUser(user);
            }
        }
        // 同步业务系统的角色数据到activiti角色表中
        List<RoleDto> roleDtos = roleService.list();
        if (!roleDtos.isEmpty()) {
            Group group = null;
            for (RoleDto roleDto : roleDtos) {
                group = new GroupEntity();
                group.setId(roleDto.getId().toString());
                group.setName(roleDto.getName());
                identityService.deleteGroup(group.getId());
                identityService.saveGroup(group);
            }
        }
        // 同步业务系统的用户角色数据到activiti的用户和用户组表中
        List<UserRoleDto> userRoleDtos = userRoleService.list();
        if (!userRoleDtos.isEmpty()) {
            for (UserRoleDto userRoleDto : userRoleDtos) {
                identityService.deleteMembership(userRoleDto.getUserId().toString(), userRoleDto.getRoleId().toString());
                identityService.createMembership(userRoleDto.getUserId().toString(), userRoleDto.getRoleId().toString());
            }
        }
        long end = System.currentTimeMillis();
        LOGGER.info("......同步数据end......");
        LOGGER.info("同步数据耗时：{}", (end - start) + "毫秒!");
    }
}
